package com.多线程基础.锁的常用情况.同步方法锁使用;
public class MyThread extends Thread{
    static int ticket=0;
    //锁对象，一定要是唯一的
    static Object object=new Object();
    @Override
    public void run() {
        while (true){
            synchronized (object){
                if(ticket<100){
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        //父类没有抛异常，所以子类稚只能try不可以抛出
                        throw new RuntimeException(e);
                    }

                    ticket++;
                    System.out.println(getName()+"正在买"+ticket+"张");
                }else {
                    break;
                }

            }
        }
    }
}
